
package com.benimetum.managers;

import com.benimetum.database.Sparql;
import com.benimetum.exceptions.DatabaseConnectException;
import com.benimetum.exceptions.DatabaseWriteOrDeleteException;
import com.benimetum.items.Course;
import com.benimetum.items.Student;
import com.benimetum.items.Teacher;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Ethem Cem Ozkan
 * @email ethemcem.ozkan@gmail.com
 * No 07110604
 */
public class TeacherManager
{
    private final static String RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    private final static String ETU="http://www.benimetum.com/benimetum.owl#";
    private final static String FOAF="http://xmlns.com/foaf/0.1/";
    
    /**
     * öğretmen oluşturur
     * @param email
     * @param password
     * @param id
     * @param firstName
     * @param lastName
     * @param profilePicture
     * @return 
     */
    public static boolean createTeacher(String email, String password, String id ,String firstName, String lastName, String profilePicture){
	  try {
		Sparql.insertProperty(ETU+"t"+id, RDF+"type", ETU+"Teacher");
		Sparql.insertLiteral(ETU+"t"+id, FOAF+"firstName", firstName);
		Sparql.insertLiteral(ETU+"t"+id, FOAF+"surname", lastName);
		Sparql.insertLiteral(ETU+"t"+id, ETU+"email", email);
		Sparql.insertLiteral(ETU+"t"+id, ETU+"id", "t"+id);
		Sparql.insertLiteral(ETU+"t"+id, ETU+"password", password);
		Sparql.insertLiteral(ETU+"t"+id, ETU+"status", "null");
		Sparql.insertLiteral(ETU+"t"+id, ETU+"profilePicture", profilePicture);
	  } catch (DatabaseConnectException ex) {
		return false;
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  }
	  return true;
    }
    
    /**
     * idsi verilen öğretmeni döndürür.
     * @param id
     * @return 
     */
    public static Teacher getTeacherById(String id) {
	  String query="select ?email ?firstName ?surname ?status ?picture "
		    + "where {?teacher rdf:type etu:Teacher. ?teacher etu:id \""+id+"\". ?teacher etu:email ?email."
		    + "?teacher foaf:firstName ?firstName. ?teacher foaf:surname ?surname. ?teacher etu:status ?status."
		    + "?teacher etu:profilePicture ?picture.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Teacher teacher=new Teacher(toList.get(0).get("email").toString(), id, toList.get(0).get("firstName").toString(), toList.get(0).get("surname").toString(), toList.get(0).get("status").toString(), toList.get(0).get("picture").toString());
	  return teacher;
    }
    
    public static boolean updateTeacherStatus(String id, String status){
	  try {
		Sparql.removeLiteral(ETU+id, ETU+"status");
		Sparql.insertLiteral(ETU+id, ETU+"status",status);
	  } catch (DatabaseWriteOrDeleteException ex) {
		return false;
	  } catch (DatabaseConnectException ex) {
		return false;
	  }
	  return true;
    }
    
    public static String getTeacherStatus(String id){
	  String query="select ?status where {?teacher rdf:type etu:Teacher. ?teacher etu:id \""+id+"\". ?teacher etu:status ?status.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(toList.get(0).get("status").toString().equals("null"))
		return null;
	  return toList.get(0).get("status").toString();
    }
    
    public static String getPassword(String id){
	  String query="select ?password "
		    + "where {?student rdf:type etu:Student. ?student etu:id \""+id+"\"."
		    + "?student etu:password ?password.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty())
		return toList.get(0).get("password").toString();
	  return null;
    }
    
    public static Teacher checkUserNamePassword(String email, String password){
	  String query="select ?id ?firstName ?surname ?status ?picture "
		    + "where {?teacher rdf:type etu:Teacher. ?teacher etu:id ?id. ?teacher etu:email \""+email+"\". ?teacher etu:password \""+password+"\"."
		    + "?teacher foaf:firstName ?firstName. ?teacher foaf:surname ?surname. ?teacher etu:status ?status."
		    + "?teacher etu:profilePicture ?picture.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty()){
		Teacher teacher=new Teacher(email, toList.get(0).get("id").toString(), toList.get(0).get("firstName").toString(), toList.get(0).get("surname").toString(), toList.get(0).get("status").toString(), toList.get(0).get("picture").toString());
		return teacher;
	  }
	  return null;
    } 
    
    public static boolean checkUserName(String email){
	  String query="select ?id "
		    + "where {?teacher rdf:type etu:Teacher. ?teacher etu:id ?id. ?teacher etu:email \""+email+"\".}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty()){
		return true;
	  }
	  return false;
    }  
    
    public static String getIdByUserName(String email){
	  String query="select ?id "
		    + "where {?teacher rdf:type etu:Teacher. ?teacher etu:id ?id. ?teacher etu:email \""+email+"\".}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  if(!toList.isEmpty()){
		return toList.get(0).get("id").toString();
	  }
	  return null;
    } 
    
    public static Course[] getTeachersCourses(String teacherId){
	  String query="select ?id ?name where{?teacher rdf:type etu:Teacher. ?teacher etu:id \""+teacherId+"\". ?teacher etu:isTeacherOf ?course. ?course etu:id ?id."
		    + "?course etu:name ?name.}";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  ArrayList<Course> courses=new ArrayList<Course>();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  while (iterator.hasNext()) {
		QuerySolution querySolution = iterator.next();
		courses.add(new Course(querySolution.get("id").toString(), querySolution.get("name").toString(), TeacherManager.getTeacherById(teacherId), CourseManager.getCoursesStudents(querySolution.get("id").toString())));
	  }
	  Course[] array=new Course[courses.size()];
	  return courses.toArray(array);
    }
    
    public static boolean isSameTeacher(String id1, String id2){
	  return id1.equals(id2);
    }
    /**
     * öğretmenin danışmanı olduğu öğrencileri çeker
     * @param teacherId
     * @return 
     */
    public static Student[] getMentorsStudents(String teacherId){
        String query="select ?email ?id ?firstName ?lastName ?status ?picture ?mentor where {?teacher rdf:type etu:Teacher. ?teacher etu:id \""+teacherId+"\". ?teacher etu:mentored ?student. "
		    + "?student etu:email ?email. ?student etu:id ?id. ?student foaf:firstName ?firstName. ?student etu:status ?status. ?student foaf:surname ?lastName. "
		    + "?student etu:profilePicture ?picture. }";
	  ResultSet search = Sparql.search(query);
	  List<QuerySolution> toList = ResultSetFormatter.toList(search);
          Sparql.close();
	  Iterator<QuerySolution> iterator = toList.iterator();
	  ArrayList<Student> students=new ArrayList<Student>();
	  while(iterator.hasNext()){
		QuerySolution next = iterator.next();
		Teacher teacher = TeacherManager.getTeacherById(teacherId);
		students.add(new Student(next.get("email").toString(), next.get("id").toString(), next.get("firstName").toString(), next.get("lastName").toString(), toList.get(0).get("status").toString(), next.get("picture").toString(), teacher));
	  }
	  Student[] array=new Student[students.size()];
	  return students.toArray(array);
    }
}
